package com.acme.bankapp.domain.bank;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by anton on 8/25/14.
 */
public class EmailQueue {
    private List<Email> list = new LinkedList<Email>();
    private volatile boolean closed = false;

    public boolean isEmpty() {
        return list.isEmpty();
    }

    public boolean isClosed() {
        return closed;
    }

    public void closeAdding() {
        this.closed = true;
    }

    // main thread
    public void add(Email e) {
        if (!closed) {
            synchronized (list) {
                list.add(e);
                list.notifyAll();
            }
        }
    }

    // Thread-0
    public Email remove() {
        synchronized (list) {
            while (list.isEmpty()) {
                try {
                    list.wait();
//                    Thread.sleep(10_000);
                } catch (InterruptedException e) { }
            }
            return list.remove(0);
        }
    }

}
